//
//  TravelAccountsViewController.m
//  TeamProject_Travel
//
//  Created by lanou3g on 16/3/31.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "TravelAccountsViewController.h"
#import "EditViewController.h"
#import "FMDBModel.h"
#import "AccountModel.h"

@interface TravelAccountsViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *generalTableView;
@property(nonatomic,strong)UITableView *detailsTableView;
@property(nonatomic,strong)NSMutableArray *generalArray;
// 数据库源
@property(nonatomic,strong)NSMutableArray *dataArray;

@end

@implementation TravelAccountsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"旅行记账";

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:(UIBarButtonSystemItemAdd) target:self action:@selector(addAction)];
    
    self.dataArray = [NSMutableArray array];
    self.dataArray = [[FMDBModel defaultFMDBModel]getAllAccount];
    
    self.generalArray = [NSMutableArray array];
    
    for (AccountModel *model in self.dataArray) {
        
    }
    
    
    self.generalTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 220) style:UITableViewStyleGrouped];
    self.detailsTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 250, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - CGRectGetHeight(self.generalTableView.frame)) style:(UITableViewStyleGrouped)];
    
    self.generalTableView.delegate = self;
    self.generalTableView.dataSource = self;
    self.detailsTableView.delegate = self;
    self.detailsTableView.dataSource = self;
    
    self.generalTableView.scrollEnabled = NO;
    
    [self.view addSubview:self.generalTableView];
    [self.view addSubview:self.detailsTableView];
    
    [self.detailsTableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if ([tableView isEqual:self.generalTableView]) {
        return 2;
    }else{
        return self.dataArray.count;
    }
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    if ([tableView isEqual:self.generalTableView]) {
        return @"消费总账";
    }else{
        return @"消费明细";
    }
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:nil];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    if ([tableView isEqual:self.generalTableView]) {
        
        if (self.dataArray != nil ) {
            if (indexPath.row == 0) {
                
                cell.textLabel.text = [NSString stringWithFormat:@"共消费 %@ "];
                cell.detailTextLabel.text = @"查看消费统计";
                
            } else {
                
            }
            
        } else {
            if (indexPath.row == 0) {
                cell.textLabel.text = @"暂无消费明细";
            }else{
                cell.textLabel.text = @"查看消费统计";
            }
        }
        
        
        
    }else{
        
        AccountModel *model = [[AccountModel alloc]init];
        model = self.dataArray[indexPath.row];
        
        NSString *type = [model.type substringFromIndex:4];
        cell.textLabel.text = [NSString stringWithFormat:@"%@ %@",model.total,type];
        NSString *time = [model.time substringFromIndex:5];
        cell.detailTextLabel.text = time;
        
    }
    return cell;
}


- (void)addAction
{
    EditViewController *editVC = [[EditViewController alloc]init];
    
    [self.navigationController pushViewController:editVC animated:YES];
}




@end
